local FONT_HGT_SMALL = getTextManager():getFontHeight(UIFont.NewSmall)

ESRRankingScoreboardItemListBox = ISScrollingListBox:derive("ESRRankingScoreboardItemListBox");

function ESRRankingScoreboardItemListBox:new (x, y, width, height)
    local o = {}
    o = ISScrollingListBox:new(x, y, width, height)
    setmetatable(o, self)
    self.__index = self

    o:setFont(UIFont.NewSmall, 3)
    o:clear()
    o.drawBorder = true
    o.drawBorder = false
    o.doDrawItem = self.doDrawItemPlayer
    o.itemheight = FONT_HGT_SMALL
    o.valueFormatter = nil
    o.namePosition = 13
    o.scrollWidth = 0

    o.firstColors = {
        { r = 0.98, g = 0.77, b = 0.03 },
        { r = 0.90, g = 0.90, b = 0.90 },
        { r = 0.66, g = 0.39, b = 0.18 },
    }
    o.descending = true
    o.minPositionWidth = 10

    return o;
end

function ESRRankingScoreboardItemListBox:fullClear()
    self:clear();
    --self.items = {}
    --self.selected = -1;
    --self.itemheightoverride = {}
    --self.count = 0;
end

function ESRRankingScoreboardItemListBox:onMouseDown(x, y)
end

function ESRRankingScoreboardItemListBox:doDrawItemPlayer(y, listItem, alt)
    local a = 0.95
    local yAdjust = math.floor((self.itemheight - FONT_HGT_SMALL) / 2)
    local PADDING = 7
    local color = { r = 1, g = 1, b = 1 }
    local item = listItem.item

    if item.indexToList <= 3 then
        color = self.firstColors[item.indexToList]
    end

    self.namePosition = self.minPositionWidth

    local cut = item.cut
    local hHiddenBefore = 0
    local hHiddenAfter = 0
    if cut then
        if item.hiddenBefore and item.hiddenBefore > 0 then
            hHiddenBefore = 21
        end
        if item.hiddenAfter and item.hiddenAfter > 0 then
            hHiddenAfter = 21
        end
        y = y + hHiddenBefore + hHiddenAfter
    end

    local textValue = tostring(item.value)
    if self.valueFormatter ~= nil then
        textValue = self:valueFormatter(item.value)
    end
    if self.count > 20 then
        self.scrollWidth = self:getWidth() - self.vscroll:getX() - 3
    end
    local xValue = self:getWidth() - PADDING - self.scrollWidth - getTextManager():MeasureStringX(self.font, textValue)
    self:drawText(textValue, xValue, y + yAdjust - hHiddenAfter, color.r, color.g, color.b, a, self.font)

    if item.nameAdjusted ~= nil then
        self:drawText(item.nameAdjusted, PADDING + self.namePosition + 5, y + yAdjust - hHiddenAfter, color.r, color.g, color.b, a, self.font)
    else
        local nameWidthMax = xValue - (PADDING + self.namePosition + 5) - 5
        if nameWidthMax < 30 then
            nameWidthMax = 30
            if self.createTooltip == nil then
                self.createTooltip = ESRRankingScoreboardItemListBox.nameTooltip
            end
        end
        local nameAdjusted = item.name;
        if getTextManager():MeasureStringX(self.font, nameAdjusted) > nameWidthMax then
            while getTextManager():MeasureStringX(self.font, nameAdjusted .. "...") > nameWidthMax do
                nameAdjusted = string.sub(nameAdjusted, 1, string.len(nameAdjusted) - 1)
            end
            nameAdjusted = nameAdjusted .. "..."

        end
        item.nameAdjusted = nameAdjusted;
        self:drawText(item.nameAdjusted, PADDING + self.namePosition + 5, y + yAdjust - hHiddenAfter, color.r, color.g, color.b, a, self.font)
    end
    if cut then
        local xCut = self.namePosition - getTextManager():MeasureStringX(self.font, "... ") + PADDING + 5
        if xCut < 0 then
            xCut = PADDING
        end

        if hHiddenBefore > 0 then
            self:drawText("... " .. getText("UI_GSBQoLRankings_more") .. " " .. tostring(item.hiddenBefore), xCut, y + yAdjust - 17 - hHiddenAfter, 1, 1, 1, 0.3, self.font)
        end
        if hHiddenAfter > 0 then
            self:drawText("... " .. getText("UI_GSBQoLRankings_more") .. " " .. tostring(item.hiddenAfter), xCut, y + yAdjust - (self.itemheight / 2) + 3, 1, 1, 1, 0.3, self.font)
        end
    end

    local positionText = tostring(item.indexToList)
    if item.index ~= item.indexToList then
        a = 0.4
    end
    self:drawText(positionText, PADDING + self.namePosition - getTextManager():MeasureStringX(self.font, positionText), y + yAdjust - hHiddenAfter, color.r, color.g, color.b, a, self.font)

    local extra = 0
    if self.count > 3 then
        local next = nil
        if item.index + 1 <= self.count then
            next = self.items[item.index + 1].item
        end
        if next ~= nil then
            if next.hasSpace then
                extra = 8
            end
        end
    end

    return y + self.itemheight + extra
end

function ESRRankingScoreboardItemListBox:addItem(name, item)
    local i = {}
    i.text = name
    i.item = item
    i.tooltip = nil
    if self.createTooltip then
        i.tooltip = self:createTooltip(item)
    end
    i.itemindex = self.count + 1
    i.height = self.itemheight
    table.insert(self.items, i)
    self.count = self.count + 1
    self:setScrollHeight(self:getScrollHeight() + i.height)
    return i
end

function ESRRankingScoreboardItemListBox:nameTooltip(listItem)
    if listItem then
        return (tostring(listItem.indexToList) .. ". " .. listItem.name)
    end
    return ""
end

function ESRRankingScoreboardItemListBox:factionTooltip(listItem)
    local faction = listItem.rawData
    local color = faction.color or { r = 1, b = 1, g = 1 }
    local textColor = "<RGB:" .. tostring(color.r) .. "," .. tostring(color.g) .. "," .. tostring(color.b) .. ">"
    local text = textColor .. " " .. faction.name .. " <LINE>"
    if faction.tag then
        text = text .. textColor .. "[" .. faction.tag .. "] <LINE>"
    end

    text = text .. " <LINE>"

    if faction.killsDetailed then
        text = text .. " <RGB:1,1,1>" .. getText("UI_GSBQoLRankings_players") .. " <LINE>"

        local ordered = {}
        for k, kills in pairs(faction.killsDetailed) do
            table.insert(ordered, { name = k, value = kills })
        end
        table.sort(ordered, function(a, b)
            if a.value == b.value then
                return a.name < b.name
            else
                return a.value > b.value
            end
        end)
        for i, item in ipairs(ordered) do
            text = text .. " <RGB:1,1,1>" .. item.name .. " - " .. tostring(item.value) .. " <LINE>"
        end
    end
    return text;
end

function ESRRankingScoreboardItemListBox:formatLifetime(data)
    local ret = ""
    if (data < 1) then
        local minutes = math.floor(data * 60)
        ret = append(ret, minutes, "min")
    else
        local days = math.floor(data / 24)
        local hours = math.fmod(data, 24)
        local months = math.floor(days / 30)
        local years = math.floor(months / 12)
        days = math.fmod(days, 30)
        months = math.fmod(months, 12)
        local minutes = math.floor(math.fmod(hours, 1) * 60)
        hours = math.floor(hours)

        function append(text, value, description)
            if (value > 0) then
                if text ~= "" then
                    text = text .. " "
                end
                text = text .. tostring(value) .. description
            end
            return text
        end

        ret = append(ret, years, "y")
        ret = append(ret, months, "m")
        ret = append(ret, days, "d")
        ret = append(ret, hours, "h")
    end

    return ret
end

function ESRRankingScoreboardItemListBox:totalEntries(total)
    if total then
        local text = tostring(total)
        self.minPositionWidth = getTextManager():MeasureStringX(self.font, text)
    end
end
